let Sequelize = require('sequelize');
let Op = Sequelize.Op;

const Tang = require('./ORM'); // 数据库连接

async function Tang300Query(req, res) {
  const { contents = '', page = 1 } = req.query;
  const pageSize = 10; // 请求页数
  let whereInfo = {}; // 查询条件
  if (contents) {
    whereInfo.contents = {
      [Op.like]: `%${contents}%`
    };
  }
  // 数据库查找
  const result = await Tang.findAndCountAll({
    order: [['id', 'ASC']], // 正常升序
    where: whereInfo,
    attributes: {
      exclude: ['id']
    },
    offset: (page - 1) * pageSize,
    limit: pageSize
  });
  if (result.rows) {
    const data = {
      result: result.rows,
      pagination: {
        page,
        pageSize,
        total: result.count
      }
    };
    res.api_success(data);
  } else {
    res.api_error('数据未查找');
  }
}

module.exports = Tang300Query;
